import Vue from 'vue'
import Router from 'vue-router'
import IndexPage from "../pages/IndexPage";
import TypePage from "../pages/TypePage";
import ProductPage from "../pages/ProductPage";
import QuestionPage from "../pages/QuestionPage";
import QuestionUpdatePage from "../pages/QuestionUpdatePage";
import LoginPage from "../pages/LoginPage";
import AddUserPage from "../pages/AddUserPage";
import myaxios from "../config/myaxios";
import SearchPage from "../pages/SearchPage";
import QuestionPageAll from "../pages/QuestionPageAll";

Vue.use(Router)
const router = [
  {
    path: '/',
    name: '登录',
    component: LoginPage,
  },
  {
    path: '/index',
    name: 'HelloWorld',
    component: IndexPage,
    children: [
      {
        path: '/search',
        name: '问题搜索',
        component: SearchPage,
        meta: {
          requiresAuth: true,
          isAside: true,
          requiredRoles: ['admin', "user"]
        }
      },
      {
        path: '/type',
        name: '商品类别管理',
        component: TypePage,
        meta: {
          requiresAuth: true,
          isAside: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/product',
        name: '产品管理',
        component: ProductPage,
        meta: {
          requiresAuth: true,
          isAside: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/question/:id',
        name: '问题列表分类',
        component: QuestionPage,
        meta: {
          requiresAuth: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/questionall',
        name: '问题列表',
        component: QuestionPageAll,
        meta: {
          requiresAuth: true,
          isAside: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/questionupdate/:id/:qid',
        name: '问题修改',
        component: QuestionUpdatePage,
        meta: {
          requiresAuth: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/questionupdate/:id',// type==1 修改 type==2 增加
        name: '问题增加',
        component: QuestionUpdatePage,
        meta: {
          requiresAuth: true,
          requiredRoles: ['admin']
        }
      },
      {
        path: '/adduser',// type==1 修改 type==2 增加
        name: '用户管理',
        component: AddUserPage,
        meta: {
          requiresAuth: true,
          isAside: true,
          requiredRoles: ['admin']
        }
      }

    ]
  }
]
let vueRouter = new Router({
  routes: router
})

vueRouter.beforeEach((to, from, next) => {

  if (to.matched.some(record => record.meta.requiresAuth)) {
    console.log("执行")
    // 这个路由需要用户登录后才能访问
    // 检查用户是否已经登录
    myaxios.get("user/get/login").then(res => {

      if (res.data.code === 0) {
        console.log(res.data.data.userRole)
        // console.log(record)
        var user = res.data.data
        let requiredRoles = to.meta.requiredRoles;
        console.log(requiredRoles)
        // let requiredRoles = record.meta.requiredRoles;
        console.log("要求权限", requiredRoles)
        let userRole = user.userRole;
        if (requiredRoles.includes(userRole)) {
          next()
        } else {
          next({path: '/'});
          // this.$message.error("无权访问")
        }
      } else {
        console.log()
        next({path: '/'});
      }
    }).catch(err => {
      next({path: '/'});
      // this.$message.error("系统繁忙，请重试")
      // next({
      //   path: '/login',
      //   query: { redirect: to.fullPath }
      // })
    })
  } else {
    next()
  }
})

export default vueRouter
